import { GlobalEvent } from "../base/GlobalEvent";
import { DataMgr } from "./DataMgr";

//ID操作单一字段
export interface ifIdOpr{
    id:number,
    amount:number,
}

//ID操作坐标
export interface ifIdPos{
    id:number,
    pos:ifPosition,
}

//位置数据
export interface ifPosition {
    x:number,
    y:number,
}

//世界数据
export interface ifWorldData {
    enemyList:ifEnemyData[],
    playerList:ifPlayerData[],
    killCount:number,   //杀敌数
}

//敌人数据
export interface ifEnemyData {
    id?:number,         //id
    hp:number,          //血量
    maxHp:number,       //最大血量
    pos:ifPosition,     //位置
}

//角色数据
export interface ifPlayerData {
    id?:number,         //id
    hp:number,          //血量
    maxHp:number,       //最大血量
    level:number,       //等级
    pos?:ifPosition,    //位置
    exp?:number,         //经验值
    nextExp?:number,     //下一级经验
}

//直接获取数据
export function getData(modelName:string){
    return DataMgr.Ins().data(modelName);
}

//绑定数据
export function bindData(modelName:string, fildeName:string = "", fun:any, host:any){
    GlobalEvent.on(GlobalEvent.Event.modelDataUpdate + modelName + fildeName, function(Event){
        fun(Event)
    }, host);
}

//更新数据
export function updateData(modelName:string, fildeName:string = "") {
    return (target, methodName: string, descriptor: PropertyDescriptor) => {
        var originalMethod = descriptor.value
        descriptor.value = function(...args:any[]){
            var data = originalMethod.apply(this, args);
            var Id = data && (data.idKey != null) ? data.idKey : "";
            GlobalEvent.emit(GlobalEvent.Event.modelDataUpdate + modelName + fildeName + Id, data);
        }
    }
}

//数据操作注册
export function dataOperation(modelName:string){
    return (target, methodName: string, descriptor: PropertyDescriptor) => {
        GlobalEvent.on(GlobalEvent.Event.modelDataChg + modelName + methodName, function(Event){
            target[methodName](Event);
        },this);
    }
}